﻿using System;
using System.Windows.Media;
using System.Collections.Generic;

namespace KaKaSongGame.CtrlEx
{
    public class MyTimer
    {
        static List<DelayAction> _delayactions;
        static MyTimer()
        {
            _delayactions = new List<DelayAction>();
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        public static void DelayExcute(TimeSpan ts, Action action)
        {
            _delayactions.Add(
                new DelayAction
                {
                    ExcutingTime = DateTime.Now.Add(ts),
                    ExcutingAction = action
                });
        }

        static void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            for (int i = _delayactions.Count - 1; i >= 0; i--)
            {
                if (_delayactions[i].ExcutingTime <= now)
                {
                    _delayactions[i].ExcutingAction();
                    _delayactions.RemoveAt(i);
                }
            }
        }
    }

    public class DelayAction
    {
        public DateTime ExcutingTime { set; get; }
        public Action ExcutingAction { set; get; }
    }
}
